package com.zh.blog.controller.permission;

import com.zh.blog.common.api.Result;
import com.zh.blog.dto.LabelOptionDto;
import com.zh.blog.dto.ResourceDto;
import com.zh.blog.service.ResourceService;
import com.zh.blog.vo.ConditionVO;
import com.zh.blog.vo.ResourceVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 资源控制器
 * @author ZhengHao
 * Created At 2022/8/8.
 **/
@Api(tags = "资源模块")
@RestController
public class ResourceController {
    @Autowired
    private ResourceService resourceService;

    /**
     * 查询角色资源选项
     * @return 角色资源选项
     */
    @ApiOperation(value = "查询角色资源选项")
    @GetMapping("/admin/role/resources")
    public Result<List<LabelOptionDto>> listResourceOption(){
        return Result.ok(resourceService.listResourceOption());
    }

    /**
     * 查看资源列表
     * @param conditionVO 条件
     * @return 资源列表
     */
    @ApiOperation(value = "查看资源列表")
    @GetMapping("/admin/resources")
    public Result<List<ResourceDto>> listResource(ConditionVO conditionVO) {
        return Result.ok(resourceService.listResources(conditionVO));
    }

    /**
     * 新增或者修改资源
     * @param resourceVO 资源信息
     * @return
     */
    @ApiOperation(value = "新增或者修改资源")
    @PostMapping("/admin/resources")
    public Result<?> saveOrUpdateResource(@RequestBody @Valid ResourceVO resourceVO){
        resourceService.saveOrUpdateResource(resourceVO);
        return Result.ok();
    }

    /**
     * 删除资源
     * @param resourceId 资源ID
     * @return
     */
    @ApiOperation(value = "删除资源")
    @DeleteMapping("/admin/resources/{resourceId}")
    public Result<?> deleteResource(@PathVariable("resourceId") Integer resourceId) {
        resourceService.deleteResource(resourceId);
        return Result.ok();
    }
}